package guiMVC;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class View extends JFrame implements IView {
	private static final long serialVersionUID = 1L;

	private static final String TITLE = "Projet-AEOS-MANES-HO";
	
	private JTextArea taInstance;
	private JTextArea taSolution;
	private JTextArea taResult;
	private JButton btnGetInstance;
	private JButton btnGetSolution;
	private JButton btnGetResult;

	private JTextField txtData_set_number;

	private JTextField txtGrid_number;

	private JTextField txtTrack_number;
	
	private Controller controller;	
	private Model model = Model.getModel();

	private JCheckBox chbViewInDetail;
	
	public View() {
		super(TITLE);
		model.addView(this);
		controller = new Controller(this); //!!!This controller must be instantiated before initComponents();
		initComponents();		
	}
	
	private void initComponents() {
		setLayout(null);

		JPanel header = getHeaderContainer();		
		JPanel center = getCenterContainer();
		
		header.setBounds(0, 10, 910, 50);
		add(header);
		center.setBounds(0, 40, 910, 500);
		add(center);
		
		setSize(950, 600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
	}

	private JPanel getHeaderContainer() {
		JPanel header = new JPanel(new FlowLayout(FlowLayout.LEADING));
		header.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		
		txtData_set_number = new JTextField("2", 5);
		header.add(new JLabel("Data-set-number"));
		header.add(txtData_set_number);
		
		txtGrid_number = new JTextField("9", 5);
		header.add(new JLabel("Grid-number"));
		header.add(txtGrid_number);
		
		txtTrack_number = new JTextField("170", 5);
		header.add(new JLabel("Track-number"));
		header.add(txtTrack_number);
		
		btnGetInstance = new JButton("Read Instance");
		btnGetInstance.addActionListener(controller); //set controller
		header.add(btnGetInstance);
		
		chbViewInDetail = new JCheckBox("View in detail");
		chbViewInDetail.addActionListener(controller); //set controller
		header.add(chbViewInDetail);
		
		return header;
	}

	private JPanel getCenterContainer() {
		JPanel center = new JPanel(null);
		
		JLabel lblInstance = new JLabel("Instance file");
		lblInstance.setBounds(150, 20, 100, 30);
		center.add(lblInstance);
		
		taInstance = new JTextArea();
		JScrollPane scrollInstance = new JScrollPane(taInstance);
		scrollInstance.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		scrollInstance.setBounds(20, 50, 350, 450);
		center.add(scrollInstance);

		btnGetSolution = new JButton(">>");
		btnGetSolution.addActionListener(controller); //set controller
		btnGetSolution.setBounds(380, 250, 50, 30);
		center.add(btnGetSolution);
		
		JLabel lblSolution = new JLabel("Solution file");
		lblSolution.setBounds(530, 20, 100, 30);
		center.add(lblSolution);
		
		taSolution = new JTextArea();
		JScrollPane scrollSolution = new JScrollPane(taSolution);
		scrollSolution.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		scrollSolution.setBounds(440, 50, 250, 450);
		center.add(scrollSolution);
		
		btnGetResult = new JButton(">>");
		btnGetResult.addActionListener(controller); //set controller
		btnGetResult.setBounds(700, 250, 50, 30);
		center.add(btnGetResult);
		
		JLabel lblResult = new JLabel("Result file");
		lblResult.setBounds(810, 20, 100, 30);
		center.add(lblResult);
		
		taResult = new JTextArea();
		JScrollPane scrollResult = new JScrollPane(taResult);
		scrollResult.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		scrollResult.setBounds(760, 50, 150, 450);
		center.add(scrollResult);
		return center;
	}
	
	@Override
	public void updateView(String instanceData, String solutionData, String resultData) {
		getTaInstance().setText(instanceData);
		getTaSolution().setText(solutionData);
		getTaResult().setText(resultData);		
	}	
	
	public JTextField getTxtData_set_number() {
		return txtData_set_number;
	}

	public JTextField getTxtGrid_number() {
		return txtGrid_number;
	}

	public JTextField getTxtTrack_number() {
		return txtTrack_number;
	}

	public JTextArea getTaInstance() {
		return taInstance;
	}

	public JTextArea getTaSolution() {
		return taSolution;
	}

	public JTextArea getTaResult() {
		return taResult;
	}

	public JButton getBtnGetInstance() {
		return btnGetInstance;
	}

	public JButton getBtnGetSolution() {
		return btnGetSolution;
	}

	public JButton getBtnGetResult() {
		return btnGetResult;
	}
	
	public JCheckBox getChbViewInDetail() {
		return chbViewInDetail;
	}
}
